package com.meteor.service.user;

import com.meteor.dao.BaseDao;
import com.meteor.dao.user.UserDao;
import com.meteor.dao.user.UserDaoImpl;
import com.meteor.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService{
    //业务层只会调用dao层，所以我们要引入dao层，这样才能用dao的方法
    private UserDao userDao;
    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    public User login(String userCode, String password) {
        UserDao userDao = new UserDaoImpl();;
        Connection connection = null;
        User user = null;
        connection = BaseDao.getConnection();
        user = userDao.getLoginUser(connection, userCode);
        if(user.getUserPassword() == null ? password == null : user.getUserPassword().equals(password)) {
            BaseDao.closeResource(connection,null,null);
            return user;
        }else {
            BaseDao.closeResource(connection,null,null);
            System.out.println("密码错误");
            return null;
        }
    }

    public boolean updatePwd(int id, String password) {
        Connection conn = null;
        conn = BaseDao.getConnection();
        boolean flag = false;
        try {
            if(userDao.updatePwd(conn,id,password) > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(conn,null,null);
        }
        return flag;
    }
}
